//==============================================================================
//
// File: CFavoriteList.cpp
// Project: Browser
// Author(s): Matthias Van Eeghem (matthias@van-eeghem.com)
// License: See LICENSE in root directory
//
// This class holds all methods related to the favorites list. This list holds all
// of the favorite servers of the user.
//
//==============================================================================

#include "Constants.h"
#include "CFavoriteList.h"
#include "ui_MainWindow.h"

MainWindow * CFavoriteList::m_pMainWindow;

// This method initialises the FavoritesList treeview
void CFavoriteList::InitialiseFavoritesList(MainWindow * parent)
{
    m_pMainWindow = parent;

    // Set the column widths
    parent->ui->FavoritesList->setColumnWidth(COLUMN_HOSTNAME, 200);
    parent->ui->FavoritesList->setColumnWidth(COLUMN_VERSION, 150);

    // Set a custom context menu
    parent->ui->FavoritesList->addAction(parent->ui->actionConnect);
    parent->ui->FavoritesList->addAction(parent->ui->actionDelete_favorite);
    parent->ui->FavoritesList->setContextMenuPolicy(Qt::ActionsContextMenu);
}

// This method adds a favorite server
void CFavoriteList::AddFavorite(QString serverIP, bool addToRegistry)
{
    // Split the serverIP into IP and port
    QStringList ServerInfo = serverIP.split(':');

    // Initialise the CServer to an invalid server
    CServer Server;
    Server.m_sIP = ServerInfo[0];
    Server.m_sPort = ServerInfo[1];
    Server.m_sVersion = "No version found";
    Server.m_sHostname = serverIP;
    Server.m_iPlayers = 0;
    Server.m_iMaxPlayers = 0;

    // Add it to the favorites listserver
    m_pMainWindow->m_lFavoritesList.append(Server);

    // Add the item to the favorites treeview list
    QTreeWidgetItem * Item = new QTreeWidgetItem(m_pMainWindow->ui->FavoritesList);
    Item->setText(COLUMN_HOSTNAME, "No hostname found");
    Item->setText(COLUMN_PLAYERS, "0/0");
    Item->setText(COLUMN_VERSION, "No version found");
    Item->setText(COLUMN_IP, Server.m_sIP + ":" + Server.m_sPort);
    m_pMainWindow->ui->FavoritesList->addTopLevelItem(Item);

    // If the addToRegistry bool is set to true, save it to the registry
    if(addToRegistry)
    {
        // Read the favorites
        QSettings settings;
        QString FavoritesList = settings.value("FavoritesList").value<QString>();

        // If no favorites have been found yet don't add the | string seperator
        FavoritesList = (FavoritesList.length() == 0) ? serverIP : FavoritesList + "|" + serverIP;

        // Save the new favorites
        settings.setValue("FavoritesList", FavoritesList);
    }
}

// Loads all favorites from the registry
void CFavoriteList::LoadFavorites()
{
    // Read the favorites
    QSettings settings;
    QString FavoritesList = settings.value("FavoritesList").value<QString>();

    // Split the favorites string
    QStringList FavoritesArray = FavoritesList.split(SERVERDATA_SEPERATOR);
    QString FavoriteServer;

    // Loop through the array
    foreach(FavoriteServer, FavoritesArray)
    {
        // Check if it's a valid server
        if(FavoriteServer.length() > 0)
        {
            CFavoriteList::AddFavorite(FavoriteServer, false);
        }
    }
}

// This method removes the currently selected TreeView item
void CFavoriteList::RemoveCurrentlySelectedItem()
{
    // Get the current index
    int iSelectedIndex = m_pMainWindow->ui->FavoritesList->currentIndex().row();

    // Is the index valid?
    if(iSelectedIndex != -1)
    {
        // Remove the IP from the registry
        QSettings settings;
        QString FavoritesList = settings.value("FavoritesList").value<QString>();
        QString NewList = FavoritesList.replace(m_pMainWindow->ui->FavoritesList->currentItem()->text(3), "");
        settings.setValue("FavoritesList", NewList.replace("||", "|"));

        // Remove the server from the QList
        m_pMainWindow->m_lFavoritesList.removeAt(iSelectedIndex);
        // Remove the server from the TreeView
        m_pMainWindow->ui->FavoritesList->model()->removeRow(iSelectedIndex);
    }
}

bool CFavoriteList::DoesFavoriteExist(QString ServerIP)
{
    // We want to make sure the IP isn't already added to the favorites list
    for(int i = 0; i < m_pMainWindow->m_lFavoritesList.size(); i++)
    {
        // Get the server at index i
        CServer Server = m_pMainWindow->m_lFavoritesList.at(i);

        // If the IP is the same, the IP already exists
        if((Server.m_sIP + ":" + Server.m_sPort) == ServerIP)
            return true;
    }

    return false;
}
